package com.zzw.service;

import com.zzw.entity.OauthCode;
import com.zzw.entity.OauthRefreshToken;
import com.zzw.mapper.dao.OauthAccessTokenMapper;
import com.zzw.mapper.dao.OauthCodeMapper;
import com.zzw.mapper.dao.OauthRefreshTokenMapper;
import com.zzw.util.MyMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service
@CacheConfig(cacheNames = "oauthRefreshTokenService")
public class OauthRefreshTokenService {
    @Autowired
    protected OauthRefreshTokenMapper mapper;

    @CachePut(key = "#p0.id", unless = "#result eq null")  //#p0表示第一个参数
    public OauthRefreshToken insert(OauthRefreshToken oauthRefreshToken) {
        mapper.insert(oauthRefreshToken);
        return oauthRefreshToken;
    }

    @CachePut(key = "#p0.id", unless = "#result eq null")  //#p0表示第一个参数
    public OauthRefreshToken update(OauthRefreshToken oauthRefreshToken) {
        mapper.updateByPrimaryKey(oauthRefreshToken);
        return oauthRefreshToken;
    }
    @CacheEvict(key = "#p0.id")  //删除缓存名称为userCache,key等于指定的id对应的缓存
    public int delete(OauthRefreshToken oauthRefreshToken) {
        return mapper.delete(oauthRefreshToken);
    }

    @Cacheable(key = "#p0.id", unless = "#result eq null")  //#p0表示第一个参数
    public OauthRefreshToken select(OauthRefreshToken oauthRefreshToken) {
        return mapper.selectOne(oauthRefreshToken);
    }
    @Cacheable(key = "#p0", unless = "#result eq null")  //#p0表示第一个参数
    public OauthRefreshToken findByTokenId(String tokenId) {
        return mapper.findByTokenId(tokenId);
    }
    @CacheEvict(key = "#p0")  //#p0表示第一个参数
    public int deleteByTokenId(String tokenId) {
        return mapper.deleteByTokenId(tokenId);
    }
    @CacheEvict(allEntries=true)
    public int deleteAll() {
        return mapper.delete(null);
    }
    @CacheEvict(allEntries=true)
    public int deleteByIds(String ids) {
        return mapper.deleteByIds(ids);
    }
}
